/*
  Copyright © 2010  Rodrigo Peixoto
  Copyright © 2010  Camilo Costa Campos
  Copyright © 2010  Willian Victor Silva

      This file is part of Touché.

      Touché is free software: you can redistribute it and/or modify
      it under the terms of the GNU Lesser General Public License as published
      by the Free Software Foundation, either version 3 of the License, or
      (at your option) any later version.

      This program is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      GNU Lesser General Public License for more details.

      You should have received a copy of the GNU Lesser General Public License
      along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */

#ifndef COLABORATIVEMANAGER_H
#define COLABORATIVEMANAGER_H

#include <QObject>
#include <QXmppClient.h>
#include <QXmppConfiguration.h>
#include <QXmppRoster.h>
#include <QXmppPresence.h>
#include <QXmppMessage.h>
//#include <QStringList>
#include <QMap>
#include <QList>
#include "channel.h"
#include "contact.h"
#include "xmlreader.h"
#define TOUCHE_SHOULD_SKIP_THIS
#include "toucheelement.h"

namespace Touche
{
    class ColaborativeManager: public QObject
    {
        Q_OBJECT

        QXmppClient * m_client;
        bool m_connected;
        QString m_login;
        QString bareJid;
        QString m_password;
        QStringList m_contacts;
        QMap<QString, Channel*> m_channels;
        QString m_joinedChannel;
        QString m_channel;
        QString m_channelPass;
    public:


        ColaborativeManager(QObject *parent=0);
        ~ColaborativeManager();
        void connectToServer(QString login, QString password);
        bool isConnected();
        QStringList contacts();
        void setContacts(QStringList);
        void clearContacts();
        QMap<QString, Channel*> channels();
        void setChannels(QMap<QString, Channel*>);
        QString joinedChannel();
        bool isOnline(QString bareJid);
        QStringList allBuddies();
        void removeUserFromChannels(QString bareJid);
        void sendMessageToUser(QString bareJid, QString message);
        void sendMessageToUser(QXmppMessage message);
        void sendMessageToChannel(QString channelName, QString message);
        void disconnectAccount();
        void createChannel(QString channel, QString channelPass);
        bool enterChannel(QString channel, QString pass = "");
        QString getChannel();
        QString getLogin();
        void clearChannel();

    public slots:
        void onElementAddedByMe(Touche::Element *element);
        void onLinkAddedByMe(const QUuid &uuid1, const QUuid &uuid2);
        void lockElement(const QUuid &uuid);
        void unlockElement(const QUuid &uuid);
        void killElement(const QUuid &uuid);
        void killLink(const QUuid &uuid1, const QUuid &uuid2);

    private slots:
        void setAsConnected();
        void setAsDisconnected();
        void presenceChanged(QString, QString);
        void onPresenceReceived(QXmppPresence);
        void updateContacts();

        void addBuddy(const QString& bareJid);
        void removeBuddy(const QString& bareJid);

    signals:
        void connectionError(QXmppClient::Error);
        void contactsChanged();
        void channelsChanged();
        void messageReceived(const QXmppMessage&);

        //signals de aviso sobre inicio e fim de edição
        void startEditing(QUuid uuid);
        void finishEditing(QUuid uuid);
        void connected();
        void disconnected();
    };
}
#endif // COLABORATIVEMANAGER_H
